package main

import (
	"fmt"
	"sync"
)

//import (
//	"encoding/json"
//	"fmt"
//	"net/http"
//	"sort"
//)
//
//var (
//	client_id     = "fsfsf"
//	client_secret = ""
//	result_url    = "https://www"
//)
//
//func maisn() {
//	//url := CodeUrl("29197e00deb54c5fb69922273e937152","https://baidu.com")
//	res, err := http.Get("https://baidu.com")
//	if err != nil {
//		panic(err)
//	}
//	data := make([]byte, 1024)
//	res.Body.Read(data)
//	fmt.Println(string(data))
//}
//
//func CodeUrl(client_id, result_url string) string {
//	url := "http://jinbao.pinduoduo.com/open.html"
//	//client_id
//	url += "?client_id=" + client_id
//	//授权类型为CODE
//	url += "&response_type=code"
//	//授权回调地址
//	url += "&redirect_uri=" + result_url
//	return url
//}
//
///**
// * 获取访问令牌（access_token）
// * 正式环境：http://open-api.pinduoduo.com/oauth/token
// *  参考    http://open.pinduoduo.com/#/document
// */
//func main() {
//	//url :="http://open-api.pinduoduo.com/oauth/token"
//	params := make(map[string]string)
//
//	params["client_id"] = client_id
//	params["grant_type"] = "authorization_code"
//	params["code"] = "2e2342"
//	params["redirect_uri"] = result_url
//	data, _ := json.Marshal(params)
//	fmt.Println(string(data))
//
//}
//
//func GetSin(params map[string]string) {
//	keys := make([]string, 0, len(params))
//	for k, v := range params {
//		if len(v) > 0 {
//			keys = append(keys, k)
//		}
//	}
//	sort.Strings(keys)
//
//	clientSecret := "TesT"
//	signOrigin := clientSecret
//	for _, k := range keys {
//		fmt.Println(k, params[k])
//		signOrigin += k + params[k]
//	}
//	signOrigin += clientSecret
//	fmt.Println(signOrigin)
//}

var w sync.WaitGroup

type LetterFreq map[rune]int

func CountLetters(strs []string, concurrency int) LetterFreq {
	m := make(map[rune]int)
	//c := make(chan bool)
	worker := make(chan bool, concurrency)
	ich := make(chan rune, concurrency)

	go func() {
		for _, str := range strs {
			w.Add(1)
			worker <- true
			go producer(str, ich, worker)
		}

		w.Wait()
		close(ich)
	}()

	for {
		for i := 0; i < concurrency; i++ {
			go func() {

			}()
		}
		a, ok := <-ich
		fmt.Println(ok)
		if !ok {
			break
		}
		m[a]++
		fmt.Println(a)
		//select {
		//case a := <-ich:
		//	fmt.Println(string(a))
		//case <- c:
		//	fmt.Println("ccccccccc")
		//	return nil
		//default:
		//
		//}
	}

	return m
}

func producer(str string, ich chan rune, worker chan bool) {
	defer w.Done()
	<-worker
	for _, s := range str {
		ich <- s
	}
}
func consumer() {

}

func main() {
	strs := []string{"aab", "abc", "bc"}
	fmt.Println(CountLetters(strs, 5))

}

func quickSort(num []int, hig, low int) {
	if hig <= low {
		return
	}

	mid := low
	for hig > mid {

	}
}
